#Configuration Makefile

#Relative path to the directory containing the source directory
CELLOS = $(shell pwd)
ARCH ?= ppc32
TARGET ?= ppc405
PLATFORM ?= taihu

uTEXT_BASE = 0x0

#---------------------------------------------------------------------------------------------------------------------------------------
#                                                      Tools Settings
#---------------------------------------------------------------------------------------------------------------------------------------
#The tools used
TOOL_DIR = $(CELLOS)/tools
GCC_NAME = powerpc-eabi
GCC_VER = 4.3.2
GCC_DIR =  /opt/freescale-4.3
GCC_EXE_PREFIX = $(GCC_DIR)/bin/$(GCC_NAME)-

CC      = $(GCC_EXE_PREFIX)gcc
CXX     = $(GCC_EXE_PREFIX)c++
AS      = $(GCC_EXE_PREFIX)as
AR      = $(GCC_EXE_PREFIX)ar
NM      = $(GCC_EXE_PREFIX)nm
RM      = rm -f
RN      = mv
CP      = cp
SHELL 	= sh
REMOVE 	= rm -f
COPY 	= cp
SED 	= sed
SIZE	= $(GCC_EXE_PREFIX)size
OBJDUMP = $(GCC_EXE_PREFIX)objdump
LD	= $(GCC_EXE_PREFIX)ld
OBJCOPY = $(GCC_EXE_PREFIX)objcopy
STRIP   = $(GCC_EXE_PREFIX)strip
DEBUGGER = $(GCC_EXE_PREFIX)gdb
QEMU_PPC = qemu-system-ppcemb
QEMU_GDB_PORT = 3421
GCC_LIBDIR = $(GCC_DIR)/lib/gcc/$(GCC_NAME)/$(GCC_VER)/nof

#---------------------------------------------------------------------------------------------------------------------------------------
#                                                       Directory Settings
#---------------------------------------------------------------------------------------------------------------------------------------

#Output Directory. This is where the objects and the final binary go
OUTPUT_DIR = $(CELLOS)/output

#This is where the kernel source can be found
KERNEL_DIR = $(CELLOS)/src

KERNEL_VPATH = include/ kernel/ lib/ drivers/ drivers/serial/ tools/

ARCH_VPATH = $(KERNEL_DIR)/arch/$(ARCH)/$(TARGET)/include/ $(KERNEL_DIR)/arch/$(ARCH)/$(TARGET)/  $(KERNEL_DIR)/arch/$(ARCH)/$(TARGET)/platforms/$(PLATFORM)/

VPATH = $(ARCH_VPATH) $(addprefix $(KERNEL_DIR)/, $(KERNEL_VPATH))

INCLUDE_DIRS = -I. $(addprefix -I, $(VPATH))

#---------------------------------------------------------------------------------------------------------------------------------------
#                                                      Compile/Link Settings
#---------------------------------------------------------------------------------------------------------------------------------------

# The Linker Script used for this configuration
LINKERSCRIPT = $(CELLOS)/src/arch/$(ARCH)/$(TARGET)/cellos.lds

#Optimization flag
OPT_FLAGS = -Os

#cc1: warning: command line option "-fno-rtti" is valid for C++/ObjC++ but not for C

#Command line arguments to compile .c files.
CFLAGS = -Wall -g $(INCLUDE_DIRS)  -D__KERNEL__ -DTEXT_BASE=$(uTEXT_BASE) -fno-exceptions -fno-builtin -msoft-float -c $(OPT_FLAGS)

#Command line arguments to compile .cpp files.
CPPFLAGS = -Wall -g $(INCLUDE_DIRS) -D__KERNEL__ -DTEXT_BASE=$(uTEXT_BASE) -fno-exceptions -fno-builtin -msoft-float -Wuninitialized -Woverloaded-virtual -c $(OPT_FLAGS)

#Command line arguments to the linker.
LDFLAGS = -o $(OUTPUT_DIR)/cellos.elf --script=$(LINKERSCRIPT) -Ttext $(uTEXT_BASE) -L$(GCC_LIBDIR) -lgcc -L$(GCC_DIR)/$(GCC_NAME)/lib -lstdc++

#Command line arguments for the gcc to assemble .S files.
ASFLAGS = -g  $(INCLUDE_DIRS) -D__ASSEMBLY__ -DTEXT_BASE=$(uTEXT_BASE) -fno-exceptions  -fno-builtin -msoft-float -Xassembler -m405 -mregnames -c

#---------------------------------------------------------------------------------------------------------------------------------------
#                                                      Makefile Inclusion
#---------------------------------------------------------------------------------------------------------------------------------------

-include $(CELLOS)/src/arch/$(ARCH)/$(TARGET)/platforms/$(PLATFORM)/platform.mk

-include $(CELLOS)/src/arch/$(ARCH)/$(TARGET)/target.mk

-include $(CELLOS)/src/os.mk

#---------------------------------------------------------------------------------------------------------------------------------------
#                                                     User Defined Make Targets
#---------------------------------------------------------------------------------------------------------------------------------------#qemu-system-ppcemb -S -s -bios output/ppc405_rom.bin -kernel output/cellos.bin -nographic -M taihu -cpu 0x51210950 -d in_asm,op,op_opt,int,exec,cpu
qemu.sh:
	@echo Generating qemu shell script...
	echo '#!/bin/sh' > qemu.sh
	echo exec $(QEMU_PPC) -L $(OUTPUT_DIR) -nographic -M ref405ep -cpu 401100c4 -kernel $(OUTPUT_DIR)/cellos.bin >> qemu.sh
	chmod 755 qemu.sh

dbg:
	@echo Debug cellos kernel with DDD on QEMU PowerPC405EP system emulation
	@echo "target remote localhost:3421">gdb_commands
	@echo "add-symbol-file output/cellos.elf 0x0000">>gdb_commands
	@echo "$(QEMU_PPC) -S -gdb tcp::3421 -L $(CELLOS)/src/arch/$(ARCH)/$(TARGET)/boot -nographic -M taihu -cpu 0x51210950 -kernel $(OUTPUT_DIR)/cellos.bin &" >start.sh
	@echo "sleep 1 ">>start.sh
	@echo "ddd --debugger powerpc-eabi-gdb --directory=$(CELLOS)/src -x gdb_commands $(OUTPUT_DIR)/cellos.elf &">>start.sh
	sh start.sh
